<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title th:text="${title}">智能客服系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        .chat-container {
            width: 800px;
            height: 600px;
            background: white;
            border-radius: 15px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            display: flex;
            flex-direction: column;
            overflow: hidden;
        }
        
        .chat-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px;
            text-align: center;
        }
        
        .chat-header h1 {
            font-size: 24px;
            margin-bottom: 5px;
        }
        
        .chat-header p {
            opacity: 0.9;
            font-size: 14px;
        }
        
        .chat-messages {
            flex: 1;
            padding: 20px;
            overflow-y: auto;
            background: #f8f9fa;
        }
        
        .message {
            margin-bottom: 15px;
            display: flex;
            align-items: flex-start;
        }
        
        .message.user {
            justify-content: flex-end;
        }
        
        .message.assistant {
            justify-content: flex-start;
        }
        
        .message-content {
            max-width: 70%;
            padding: 12px 16px;
            border-radius: 18px;
            word-wrap: break-word;
        }
        
        .message.user .message-content {
            background: #007bff;
            color: white;
        }
        
        .message.assistant .message-content {
            background: white;
            color: #333;
            border: 1px solid #e9ecef;
        }
        
        .message-time {
            font-size: 12px;
            color: #6c757d;
            margin-top: 5px;
        }
        
        .chat-input {
            padding: 20px;
            background: white;
            border-top: 1px solid #e9ecef;
        }
        
        .input-group {
            display: flex;
            gap: 10px;
        }
        
        .input-group input {
            flex: 1;
            padding: 12px 16px;
            border: 1px solid #ddd;
            border-radius: 25px;
            font-size: 14px;
            outline: none;
        }
        
        .input-group input:focus {
            border-color: #007bff;
        }
        
        .input-group button {
            padding: 12px 24px;
            background: #007bff;
            color: white;
            border: none;
            border-radius: 25px;
            cursor: pointer;
            font-size: 14px;
            transition: background 0.3s;
        }
        
        .input-group button:hover {
            background: #0056b3;
        }
        
        .input-group button:disabled {
            background: #6c757d;
            cursor: not-allowed;
        }
        
        .loading {
            display: none;
            text-align: center;
            padding: 10px;
            color: #6c757d;
        }
        
        .error-message {
            background: #f8d7da;
            color: #721c24;
            padding: 10px;
            border-radius: 5px;
            margin: 10px 0;
            display: none;
        }
        
        .welcome-message {
            text-align: center;
            color: #6c757d;
            padding: 20px;
            font-style: italic;
        }
    </style>
</head>
<body>
    <div class="chat-container">
        <div class="chat-header">
            <h1 th:text="${title}">智能客服系统</h1>
            <p>基于Spring Alibaba AI框架构建</p>
        </div>
        
        <div class="chat-messages" id="chatMessages">
            <div class="welcome-message">
                <p th:text="${welcomeMessage}">欢迎使用智能客服系统！请输入您的问题，我会尽力为您解答。</p>
            </div>
        </div>
        
        <div class="loading" id="loading">
            <p>AI正在思考中...</p>
        </div>
        
        <div class="error-message" id="errorMessage"></div>
        
        <div class="chat-input">
            <div class="input-group">
                <input type="text" id="messageInput" placeholder="请输入您的问题..." maxlength="2000">
                <button id="sendButton" onclick="sendMessage()">发送</button>
            </div>
        </div>
    </div>

    <script>
        let sessionId = null;
        let userId = 'user_' + Math.random().toString(36).substr(2, 9);
        
        // 页面加载完成后的初始化
        document.addEventListener('DOMContentLoaded', function() {
            document.getElementById('messageInput').addEventListener('keypress', function(e) {
                if (e.key === 'Enter') {
                    sendMessage();
                }
            });
        });
        
        // 发送消息
        async function sendMessage() {
            const messageInput = document.getElementById('messageInput');
            const message = messageInput.value.trim();
            
            if (!message) {
                return;
            }
            
            // 显示用户消息
            addMessage(message, 'user', '您');
            
            // 清空输入框并禁用发送按钮
            messageInput.value = '';
            setLoading(true);
            
            try {
                const response = await fetch('/api/chat/message', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        message: message,
                        sessionId: sessionId,
                        userId: userId
                    })
                });
                
                const data = await response.json();
                
                if (data.success) {
                    // 更新会话ID
                    if (data.sessionId) {
                        sessionId = data.sessionId;
                    }
                    
                    // 显示AI回复
                    addMessage(data.message, 'assistant', '智能助手');
                    hideError();
                } else {
                    showError(data.errorMessage || '发送消息失败');
                }
            } catch (error) {
                console.error('发送消息错误:', error);
                showError('网络错误，请稍后重试');
            } finally {
                setLoading(false);
            }
        }
        
        // 添加消息到聊天界面
        function addMessage(content, type, sender) {
            const messagesContainer = document.getElementById('chatMessages');
            const messageDiv = document.createElement('div');
            messageDiv.className = `message ${type}`;
            
            const now = new Date();
            const timeStr = now.toLocaleTimeString('zh-CN', { 
                hour: '2-digit', 
                minute: '2-digit' 
            });
            
            messageDiv.innerHTML = `
                <div class="message-content">
                    <div>${content}</div>
                    <div class="message-time">${sender} ${timeStr}</div>
                </div>
            `;
            
            messagesContainer.appendChild(messageDiv);
            messagesContainer.scrollTop = messagesContainer.scrollHeight;
        }
        
        // 设置加载状态
        function setLoading(loading) {
            const loadingDiv = document.getElementById('loading');
            const sendButton = document.getElementById('sendButton');
            const messageInput = document.getElementById('messageInput');
            
            if (loading) {
                loadingDiv.style.display = 'block';
                sendButton.disabled = true;
                messageInput.disabled = true;
            } else {
                loadingDiv.style.display = 'none';
                sendButton.disabled = false;
                messageInput.disabled = false;
                messageInput.focus();
            }
        }
        
        // 显示错误消息
        function showError(message) {
            const errorDiv = document.getElementById('errorMessage');
            errorDiv.textContent = message;
            errorDiv.style.display = 'block';
            
            // 5秒后自动隐藏错误消息
            setTimeout(hideError, 5000);
        }
        
        // 隐藏错误消息
        function hideError() {
            const errorDiv = document.getElementById('errorMessage');
            errorDiv.style.display = 'none';
        }
    </script>
</body>
</html>
